<?php
namespace BoxEdu\Hooks;
class AskTopicHooks extends \HooksController {

	function topic_before_save_eduAsk(&$params) {
		if (!$params['id']) {
			$user_info = BoxModel("system_member")->where(array('user_id' => $this->user_id))->find();
			if ($user_info['ask_num'] < 1) {
				exit(json_encode(array('error' => 2, 'msg' => "你的提问数已用完，请先购买提问数")));
			}
		}
	}

	function topic_insert_eduAsk(&$params) {
		$user_info = BoxModel("system_member")->where(array('user_id' => $this->user_id))->find();
		$title = $params['title'] ? $params['title'] : $params['content'];
		/*********提问数减少*******/
		$row = array(
			'user_id' => $this->user_id,
			'num' => -1,
			'title' => cutstr(htmlspecialchars($title), "50"),
			'url' => HomeUrl . U('Subject/topic_info', array('topic_id' => $params['id'])),
			'order_sn' => "",
			'type' => "ask_num",
			'event' => '00009',
			'subjectid' => intval($params['pid']),
		);
		//	dump($row);exit();
		$r = Logic('BoxSns', 'Log')->add($row);
	}

	//回答问题
	function comment_after_save_EduAsk(&$params) {
		$topicData = BoxModel("sns_topic")->where(array('id' => $params['pid']))->find();
		$title = $topicData['title'] ? $topicData['title'] : $topicData['content'];
		$user_data = BoxModel("system_member")->where(array('user_id' => $params['user_id']))->find();
		/****最后回复用户**/
		BoxModel("sns_topic")->where(array('id' => $params['pid']))->save(array('lastPostName' => $user_data['nickname'], 'lastupdate' => time()));
		$url = HomeUrl . U('Subject/topic_info', array('topic_id' => $params['pid'], 'comment_id' => $params['id']));
		/******提醒楼主***/
		if ($topicData['user_id'] != $this->user_id) {
			$NotiParams = array(
				'user_id' => $topicData['user_id'],
				'url' => $url,
				'name' => $user_data['nickname'] ? $user_data['nickname'] : "有人",
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			Logic("BoxSns", "Noti")->send("00004", $NotiParams);
		}
		/*******提醒AT***/
		$user_ids = Logic("BoxSns", "Noti")->At($content); //需要@提醒的用户ID
		unset($user_ids[$this->user_id]);
		if (!empty($user_ids)) {
			$NotiParams = array(
				'user_id' => $user_ids,
				'url' => $url,
				'name' => $user_data['nickname'] ? $user_data['nickname'] : "有人",
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			Logic("BoxSns", "Noti")->send("00005", $NotiParams);
		}
		/**********回复提醒****/

		if (!empty($params['to_comment_id']) && $params['to_user_id'] != $this->user_id) {
			$NotiParams = array(
				'user_id' => $params['to_user_id'],
				'url' => $url,
				'name' => $user_data['nickname'] ? $user_data['nickname'] : "有人",
				'title' => cutstr(htmlspecialchars($title), 50),
			);
			//	dump($NotiParams);
			Logic("BoxSns", "Noti")->send("00006", $NotiParams);
		}
		/***回复获得学分*/
		$row = array(
			'user_id' => $params['user_id'],
			'url' => $url,
			'title' => cutstr(htmlspecialchars($title), 50),
			'info' => cutstr($params['content'], 400),
			'event' => "00005",
			'subjectid' => $topicData['pid'],
		);
		//dump($row);
		$r = Logic("BoxEdu", "User")->add_xuebazi($row);
		//$r = Logic('BoxSns', 'Log')->add($row);
	}

	//评论的pid
	function comment_load_EduAsk(&$params) {
		$params['pidData'] = BoxModel("sns_topic")->where(array('id' => $params['pid']))->find();
	}
}
